<template>
  <div>
    <el-form label-width="80px" :inline="false" size="default">
      <el-form-item label="SPU 名称">
        <span></span>
      </el-form-item>
      <el-form-item label="SKU 名称">
        <el-input size="default" clearable></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input size="default" clearable></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input size="default" clearable></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input size="default" clearable type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form label-width="80px" :inline="true" size="default">
          <el-form-item
            :label="attr.attrName"
            v-for="attr in attrList"
            :key="attr.id"
          >
            <el-select placeholder="请选择" clearable>
              <el-option
                v-for="item in attr.attrValueList"
                :key="item.id"
                :label="item.valueName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form label-width="80px" :inline="true" size="default">
          <el-form-item
            :label="saleAttr.saleAttrName"
            v-for="saleAttr in saleAttrList"
            :key="saleAttr.id"
          >
            <el-select placeholder="请选择" clearable>
              <el-option
                v-for="item in saleAttr.spuSaleAttrValueList"
                :key="item.id"
                :label="item.saleAttrValueName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table border stripe :data="spuImageList" >
          <el-table-column type="selection" width="50" />
          <el-table-column label="图片">
            <template v-slot="{ row }">
              <img :src="row.imgUrl" width="100" alt="">
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName"></el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button type="primary" size="small">设为默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary">保存</el-button>
        <el-button @click="emit('changeType', 0)">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { reqAttrInfoList, type attrListType } from "@/api/attr";
import {
  reqSaleAttrSelf,
  reqSpuImageList,
  type saleAttrListType,
  type spuImgListType,
  type spuItemType,
} from "@/api/spu";
import useCategoryStore from "@/stores/category";
import { ElMessage } from "element-plus";
import { storeToRefs } from "pinia";
import { defineComponent, onMounted, ref } from "vue";

export default defineComponent({
  name: "SkuForm",
});
</script>

<script setup lang="ts">
const categoryStore = useCategoryStore();
// 拿到当前新增sku所在的spu信息
const props = defineProps<{
  spuInfo: spuItemType;
}>();
//接收切换type的自定义事件
const emit = defineEmits<{
  (event: "changeType", type: number, row?: spuItemType): void;
}>();

// 拿到category的所有的id
const { category1Id, category2Id, category3Id } = storeToRefs(categoryStore);

// 初始化数据收集平台属性列表,销售属性列表,图片列表
const attrList = ref<attrListType>([]);
const saleAttrList = ref<saleAttrListType>([]);
const spuImageList = ref<spuImgListType>([]);
const initData = async () => {
  const p1 = reqAttrInfoList(
    category1Id.value as number,
    category2Id.value as number,
    category3Id.value as number
  );
  const p2 = reqSaleAttrSelf(props.spuInfo.id as number);
  const p3 = reqSpuImageList(props.spuInfo.id as number);

  try {
    const result = await Promise.all([p1, p2, p3]);
    attrList.value = result[0];
    saleAttrList.value = result[1];
    spuImageList.value = result[2];
  } catch (e: any) {
    ElMessage.error(e.message);
  }
};

// 初始化请求数据
onMounted(() => {
  initData();
});
</script>

<style scoped></style>
